﻿using System;
using System.Data;
using System.Collections.Generic;

namespace CounterServerModel
{
    /// <summary>
    /// A class that represents a user of the system.
    /// </summary>
    public class UserInfo
    {
        /// <summary>
        /// The ID of the user.
        /// </summary>
        public int ID { get; private set; }

        /// <summary>
        /// The username of the user.
        /// </summary>
        public string Username { get; set; }

        /// <summary>
        /// The permissions of the user.
        /// </summary>
        public eUserPermissions UserPermissions { get; set; }

        /// <summary>
        /// The phone number of the user.
        /// </summary>
        public string Phone { get; set; }

        /// <summary>
        /// The mobile phone number of the user.
        /// </summary>
        public string MobilePhone { get; set; }

        /// <summary>
        /// The Email of the user.
        /// </summary>
        public string Email { get; set; }

        /// <summary>
        /// The Password of the user.
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// Accounts associated with the User.
        /// </summary>
        public List<int> AccountsAssociatedWith { get; set; }

		/// <summary>
		/// Returns true if this is a new user witout an ID (to be created)
		/// </summary>
		public bool isNew 
		{
			get { return ID == -1; }
		}
        
		public UserInfo(int i_ID, string i_Username)
        {
            Username = i_Username;
            ID = i_ID;

            AccountsAssociatedWith = new List<int>();
        }

        public UserInfo(string i_Username) : this(-1, i_Username){}

		public UserInfo() : this(-1,null){}

        /// <summary>
        /// Creates a UserInfo based on the given DataRow.
        /// </summary>
        /// <param name="i_DataRow">The DataRow to create the user from.</param>
        /// <param name="i_AccountsUsersTable">The Table of accounts that are associated with this user.</param>
        /// <returns>A UserInfo based on the given DataRow.</returns>
        public static UserInfo CreateFromUserRowAndAccountsColumn(DataRow i_UserRow, DataTable i_AccountsUsersTable)
        {
            UserInfo userInfo = new UserInfo(int.Parse(i_UserRow["UserID"].ToString()), i_UserRow["Username"].ToString());
            userInfo.UserPermissions = (eUserPermissions)int.Parse(i_UserRow["Permissions"].ToString());
            userInfo.Phone = i_UserRow["Phone"].ToString();
            userInfo.MobilePhone = i_UserRow["MobilePhone"].ToString();
            userInfo.Email = i_UserRow["Email"].ToString();
            userInfo.Password = i_UserRow["Password"].ToString();
            userInfo.AccountsAssociatedWith.AddRange(GetAssociatedAccountInfos(i_AccountsUsersTable));

            return userInfo;
        }

        private static List<int> GetAssociatedAccountInfos(DataTable i_AccountsUsersTable)
        {
            List<int> accountInfos = new List<int>();

            foreach (DataRow row in i_AccountsUsersTable.Rows)
            {
                accountInfos.Add(int.Parse(row["AccountID"].ToString()));
            }

            return accountInfos;
        }
    }
}
